import 'package:dio/dio.dart';
import 'package:sonora/models/sj_add_friend_body.dart';
import 'package:sonora/models/sj_block_list_item_data.dart';
import 'package:sonora/models/sj_broad_caster_response_data.dart';
import 'package:sonora/models/sj_call_create_body.dart';
import 'package:sonora/models/sj_config_data.dart';
import 'package:sonora/models/sj_content_search_body.dart';
import 'package:sonora/models/sj_friend_list_body.dart';
import 'package:sonora/models/sj_friend_list_item_data.dart';
import 'package:sonora/models/sj_good_search_body.dart';
import 'package:sonora/models/sj_good_search_item_data.dart';
import 'package:sonora/models/sj_insert_record_body.dart';
import 'package:sonora/models/sj_login_response_data.dart';
import 'package:sonora/models/sj_recharge_create_body.dart';
import 'package:sonora/models/sj_recharge_create_data.dart';
import 'package:sonora/models/sj_recharge_payment_body.dart';
import 'package:sonora/models/sj_remove_block_body.dart';
import 'package:sonora/models/sj_response_data.dart';
import 'package:sonora/models/sj_review_consume_body.dart';
import 'package:sonora/models/sj_save_userInfo_body.dart';
import 'package:sonora/models/sj_un_friend_body.dart';
import 'package:sonora/models/sj_update_avatar_body.dart';
import 'package:sonora/models/sj_valid_token_body.dart';
import 'package:sonora/models/sj_wall_search_body.dart';
import 'package:sonora/network/sj_network.dart';
import 'package:sonora/services/sj_api_client.dart';
import 'package:sonora/utils/sj_device_utils.dart';
import 'package:sonora/utils/sj_storage_utils.dart';

class SjApiHomeService {
  final SjApiClient client;
  SjApiHomeService(this.client);

  // SjApiHomeService._internal(this._client);

  // static final SjApiHomeService _instance =
  //     SjApiHomeService._internal(SjApiClient(clientDio() as Dio));

  // factory SjApiHomeService() {
  //   return _instance;
  // }

  static Future<Dio> clientDio() async {
    Dio dio = SjNetwork().clientDio;
    final SjLoginData? loginData = await SjStorageUtils.getLoginInfo();
    final String? token = loginData?.token;
    dio.options.headers.addAll({
      'Authorization':
          'Bearer${token ?? 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODI3MzY0NjM0NzE0MzA4NjA4IiwidXNlcl90eXBlIjoxLCJleHAiOjQ4ODAyMjU5NDMsImNyZWF0ZWQiOjE3MjQ1NTIzNDMxOTB9.qRym7OgHlaYZnpmSaDQlDAI-6WHkZ99iSnQ5onkaWUX0FbFCQ2MOHVMs9HzmIAvLU67Kk_vB7zOdaR_B3-YRDA'}'
    
    });
    return dio;
  }

  // SjApiClient get client => _client;
  static const String tempUserId = '1739995711774261248';

  Future<SjLoginResponseData> appleLogin(String toke) async {
    int oauthType = 3;
    SjLoginResponseData response = await client.appleLogin(oauthType, toke);
    return response;
  }

  Future<SjBroadCasterResponseData> getWallSearch(int limit, int page) async {
    var category = "Popular";
    var isPageMode = true;
    var isRemoteImageUrl = true;
    var tag = 'All';

    SjBroadCasterResponseData response = await client.getWallSearch(
        SjWallSearchBody(
            category: category,
            isPageMode: isPageMode,
            isRemoteImageUrl: isRemoteImageUrl,
            tag: tag,
            limit: limit,
            page: page));
    return response;
  }

  Future<SjResponseData> validToken() async {
    SjLoginData? loginData = await SjStorageUtils.getLoginInfo();
    String? token = loginData?.token;
    SjValidTokenBody validTokenBody = SjValidTokenBody(token: token);
    SjResponseData response = await client.validToken(validTokenBody);
    return response;
  }

  Future<SjResponseData> getUserInfo() async {
    SjLoginData? loginData = await SjStorageUtils.getLoginInfo();
    String? userId = loginData?.userInfo?.userId;
    SjResponseData response =
        await client.getUserInfo(userId ?? tempUserId);
    return response;
  }

  Future<SjResponseData> saveUserInfo() async {
    SjLoginData? loginData = await SjStorageUtils.getLoginInfo();
    String? userId = loginData?.userInfo?.userId;
    SjSaveUserInfoBody saveUserInfoBody = SjSaveUserInfoBody(
        userId: userId ?? tempUserId,
        nickname: 'ABCD',
        about: '1234',
        language: 'zh',
        country: 'HK',
        birthday: '2000-01-01');
    SjResponseData responseData = await client.saveUserInfo(saveUserInfoBody);
    return responseData;
  }

  Future<SjResponseData> getOssPolicy() async {
    SjResponseData responseData = await client.getOssPolicy();
    return responseData;
  }

  Future<SjResponseData> getStrategy() async {
    SjResponseData responseData = await client.getStrategy();
    return responseData;
  }

  Future<SjResponseData> getRongcloudToken() async {
    SjResponseData responseData = await client.getRongcloudToken();
    return responseData;
  }

  Future<SjResponseData> updateAvatar() async {
    SjUpdateAvatarBody updateAvatarBody = SjUpdateAvatarBody(avatarPath: '');
    SjResponseData responseData = await client.updateAvatar(updateAvatarBody);
    return responseData;
  }

  Future<SjResponseData> addFriend(String followUserId) async {
    SjAddFriendBody addFriendBody =
        SjAddFriendBody(followUserId: followUserId);
    SjResponseData responseData = await client.addFriend(addFriendBody);
    return responseData;
  }

  Future<SjResponseData> unFriend(String followUserId) async {
    SjUnFriendBody unFriendBody =
        SjUnFriendBody(followUserId: followUserId);
    SjResponseData responseData = await client.unfriend(unFriendBody);
    return responseData;
  }

  Future<SjResponseData> insertRecord(String? broadcasterId, String? complainCategory, String? complainSub) async {
    SjInsertRecordBody insertRecordBody = SjInsertRecordBody(
        broadcasterId: broadcasterId,
        complainCategory: complainCategory,
        complainSub: complainSub);
    SjResponseData responseData = await client.insertRecord(insertRecordBody);
    return responseData;
  }

  Future<SjResponseData> removeBlock(String blockUserId) async {
    SjRemoveBlockBody removeBlockBody =
        SjRemoveBlockBody(blockUserId: blockUserId);
    SjResponseData responseData = await client.removeBlock(removeBlockBody);
    return responseData;
  }

  Future<List<SjBlockListItemData>> blockList() async {
    SjResponseData responseData = await client.blockList();
    List<dynamic> data = responseData.data;
    List<SjBlockListItemData> list =
        data.map((e) => SjBlockListItemData.fromJson(e)).toList();
    return list;
  }

  Future<List<SjFriendListItemData>> friendList() async {
    SjFriendListBody friendListBody = SjFriendListBody(limit: 20, page: 1);
    SjResponseData responseData = await client.frientList(friendListBody);
    List<dynamic> data = responseData.data;
    List<SjFriendListItemData> list =
        data.map((e) => SjFriendListItemData.fromJson(e)).toList();
    return list;
  }

  Future<SjResponseData> getUserCoins() async {
    SjResponseData responseData = await client.getUserCoins();
    return responseData;
  }

  Future<List<SjGoodSearchItemData>> goodsSearch() async {
    SjGoodSearchBody goodSearchBody =
        SjGoodSearchBody(payChannel: 'IAP', isIncludeSubscription: false);
    SjResponseData responseData = await client.goodsSearch(goodSearchBody);
    List<dynamic> data = responseData.data;
    List<SjGoodSearchItemData> list =
        data.map((e) => SjGoodSearchItemData.fromJson(e)).toList();
    return list;
  }

  Future<List<String>> getContentSearch() async {
    SjContentSearchBody contentSearchBody = SjContentSearchBody(type: 1);
    SjResponseData responseData =
        await client.getContentSearch(contentSearchBody);
    List<String> list = responseData.data;
    return list;
  }

  Future<SjResponseData> videoCallMatch() async {
    SjResponseData responseData = await client.videoCallMatch();
    return responseData;
  }

  Future<SjResponseData> videoCallCreate() async {
    String uuid = await SjDeviceUtils.getUuid();
    SjCallCreateBody callCreateBody = SjCallCreateBody(
        supportVideoSdks: [1],
        toUserId: tempUserId,
        callType: 2,
        callSource: 12,
        isFree: 'true',
        clientSessionId: uuid);
    SjResponseData responseData = await client.videoCallCreate(callCreateBody);
    return responseData;
  }

  Future<SjRechargeCreateData> rechargeCreate() async {
    SjRechargeCreateBody rechargeCreateBody = SjRechargeCreateBody(
        entry: '', goodsCode: '61212', payChannel: 'IAP', source: '');
    SjResponseData responseData =
        await client.rechargeCreate(rechargeCreateBody);
    return SjRechargeCreateData.fromJson(responseData.data);
  }

  Future<SjResponseData> rechargePayment() async {
    SjRechargePaymentBody rechargePaymentBody = SjRechargePaymentBody(
      type: '1',
      orderNo: '6cf4c5957ef54323a044895f3f900c01'
    );
    SjResponseData responseData = await client.rechargePayment(rechargePaymentBody);
    return responseData;
  }

  Future<SjResponseData> reviewModeConsume() async {
    SjReviewConsumeBody reviewConsumeBody = SjReviewConsumeBody(
      outlay: 1,
      source: 'iap'
    );
    SjResponseData responseData = await client.reviewModeConsume(reviewConsumeBody);
    return responseData;
  }

  Future<SjConfigData?> getAppConfig() async {
    String? ver;
    SjConfigData? config = await SjStorageUtils.getAppConfig();
    if (config != null && config.ver != null) {
      ver = config.ver;
    }
    SjResponseData response = await client.getAppConfig(ver ?? '0');
    if (response.data != null && response.data is Map<String, dynamic>) {
      return SjConfigData.fromJson(response.data);
    }
    return null;
  }
}
